package com.suzj.extraenchantments;


import com.mojang.logging.LogUtils;
import com.suzj.extraenchantments.enchant.EnchantRegistry;
import com.suzj.extraenchantments.item.ModCreativeTabs;
import com.suzj.extraenchantments.item.ItemRegistry;
import com.suzj.extraenchantments.network.Networkhandler;
import com.suzj.extraenchantments.network.PacketPlayerMotion;
import com.suzj.extraenchantments.particle.ParticleRegistry;
import com.suzj.extraenchantments.potion.ModEffects;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.*;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.world.item.alchemy.PotionUtils;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.server.ServerStartingEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.slf4j.Logger;

import java.util.ArrayList;

@Mod(ExtraEnchantments.MODID)
public class ExtraEnchantments {
    public static final String MODID = "extraenchantments";
    public static final Logger LOGGER = LogUtils.getLogger();

    public static final ArrayList<ItemStack> kachi = new ArrayList();

    public ExtraEnchantments(FMLJavaModLoadingContext context) {
        IEventBus modEventBus = context.getModEventBus();

        modEventBus.addListener(this::commonSetup);


        ModEffects.EFFECTS.register(modEventBus);
        ItemRegistry.ITEMS.register(modEventBus);
        EnchantRegistry.ENCHANTMENTS.register(modEventBus);
        ModCreativeTabs.CREATIVE_MODE_TABS.register(modEventBus);
        ParticleRegistry.PARTICLE_TYPES.register(modEventBus);
        MinecraftForge.EVENT_BUS.register(this);
        Networkhandler.INSTANCE.registerMessage(1, PacketPlayerMotion.class, PacketPlayerMotion::encode, PacketPlayerMotion::decode, PacketPlayerMotion::handle);

        context.registerConfig(ModConfig.Type.COMMON, Config.SERVER_CONFIG);
    }

    public static void addEnchantmentBook(Enchantment e, int level) {
        ItemStack book = new ItemStack(Items.ENCHANTED_BOOK);
        book.enchant(e, level);
        kachi.add(book);
    }

    private static void addItem(Item item) {
        kachi.add(new ItemStack(item));
    }

    private static void addItem(Item item, int count) {
        ItemStack itemStack = new ItemStack(item);
        itemStack.setCount(count);
        kachi.add(itemStack);
    }

    public static void setItem(Item item) {
        addItem(item);
    }

    public static void setItem(Item item, int count) {
        addItem(item, count);
    }

    private static void addItemStack(ItemStack itemStack) {
        kachi.add(itemStack);
    }

    private static void addPotion(Potion potion) {
        ItemStack itemStack = PotionUtils.setPotion(new ItemStack(Items.SPLASH_POTION), potion);
        itemStack.getOrCreateTag().putInt("Amplifier", 1);
        addItemStack(itemStack);
    }

    private static void addPotion(Potion potion, int level) {
        ItemStack itemStack = PotionUtils.setPotion(new ItemStack(Items.SPLASH_POTION), potion);
        itemStack.getOrCreateTag().putInt("Amplifier", level);
        addItemStack(itemStack);
    }

    private void commonSetup(final FMLCommonSetupEvent event) {

    }



    @SubscribeEvent
    public void onServerStarting(ServerStartingEvent event) {
    }



    public static ResourceLocation res(String name) {
        return ResourceLocation.fromNamespaceAndPath(MODID, name);
    }
}
